<!DOCTYPE HTML>
<html>
<head th:include="fragments/_header :: _head('添加资源')"></head>
<link rel="stylesheet" href="/lib/zTree/v3/css/metroStyle/metroStyle.css">
<body>
<article class="page-container">
    <input type="hidden" id="roleId" name="roleId" th:value="${roleId}">
    <div class="row cl">
        <ul id="treeDemo" class="ztree"></ul>
    </div>
    <div class="row cl">
        <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
            <input id="saveBtn" class="btn btn-primary radius" type="button" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
        </div>
    </div>
</article>
<div th:include="fragments/_footer :: _footer"></div>
<script type="text/javascript" src="/lib/zTree/v3/js/jquery.ztree.all-3.5.min.js"></script>
<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript">
    $(document).ready(function () {
        var setting = {
            check: {
                enable: true,
                chkboxType: {"Y": "ps", "N": "ps"}
            },
            data: {
                simpleData: {
                    enable: true
                }
            }
        };
        $.ajax({
            url: '/resources/getResourcesTreeData',
            type: 'get',
            data: {roleId: $("#roleId").val()},
            dataType: 'json',
            success: function (json) {
                // var json = JSON.parse(jsondata);
                if (json.result) {
                    $.fn.zTree.init($("#treeDemo"), setting, json.data);
                }
            }
        });
        $('#saveBtn').click(function (e) {
            var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
            var nodes = treeObj.getCheckedNodes();
            if (nodes && nodes.length > 0) {
                var resourcesIds = new Array();
                for (var index = 0; index < nodes.length; index++) {
                    var node = nodes[index];
                    resourcesIds.push(node.id);
                }
            }
            $.ajax({
                url: '/resources/role_add',
                type: 'post',
                traditional: true,
                data: {roleId: $("#roleId").val(), resourcesIds: resourcesIds},
                dataType: 'json',
                success: function (result) {
                    if (result.success) {
                        layer.msg("授权成功", {icon: 1, time: 1000});
                        parent.window.location.reload();
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            });

        });
    });
</script>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>